package services;

import java.util.Collection;
import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import repositories.CommentRepository;
import domain.Comment;
import domain.Customer;
import domain.Painting;

@Service
@Transactional
public class CommentService {

	// Managed repository

	@Autowired
	private CommentRepository commentRepository;
	

	@Autowired
	private ActorService actorService;
	// Supporting services
	
	
	
	// Other business methods

	public Comment create(Painting painting) {
		// TODO Auto-generated method stub
		Comment comment =  new Comment();
		Customer customerP = (Customer) actorService.findByPrincipal();
		Assert.isTrue(customerP instanceof Customer);
		comment.setMoment(new Date(System.currentTimeMillis()-1000));
		comment.setCustomer(customerP);
		comment.setPainting(painting);
		return comment;
	}
	public Collection<Comment> findCommentParentByPaintingId(int paintingId){
		return commentRepository.findCommentParentByPaintingId(paintingId);
	}
	
	public Collection<Comment> findCommentWithMoreResponses(){
		return commentRepository.findCommentWithMoreResponses();
	}
	
	public Comment findOne(int commentId) {
		// TODO Auto-generated method stub
		return commentRepository.findOne(commentId);
	}
	public void save(Comment comment) {
		Customer customerP = (Customer) actorService.findByPrincipal();
		Assert.isTrue(customerP instanceof Customer);
		
		commentRepository.save(comment);
	}
	public Comment create(Comment comment) {
		Comment commentNew =  new Comment();
		Customer customerP = (Customer) actorService.findByPrincipal();
		Assert.isTrue(customerP instanceof Customer);
		commentNew.setMoment(new Date(System.currentTimeMillis()-1000));
		commentNew.setCustomer(customerP);
		commentNew.setParent(comment);
		commentNew.setPainting(comment.getPainting());
		return commentNew;
	}

	
	
	
	
}
